package GUILayer;
import ControlLayer.CtrGroup;
import ModelLayer.Group;
import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;

public class GUIGroup extends javax.swing.JFrame {
    private CtrGroup ctrGroup;

    public GUIGroup() {
        initComponents();
        ctrGroup = CtrGroup.getInstance();
        loadTableContent();
    }

    private void loadTableContent() {
        ((DefaultTableModel)tblGroupList.getModel()).setRowCount(0);
        ArrayList<Group> groupList = ctrGroup.getGroupList();
        for (Group group : groupList) {
            String customerIDs = new String();
            for (Integer customerID : group.getCustomerIDs()) {
                customerIDs += customerID + ", ";
            }
            ((DefaultTableModel)tblGroupList.getModel()).addRow(
                new Object [] {
                    group.getIDGroup(),
                    group.getReduction(),
                    group.getDateAdd(),
                    group.getDateUpd(),
                    group.getName(),
                    customerIDs
                }
            );
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrlGroupList = new javax.swing.JScrollPane();
        tblGroupList = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tblGroupList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "id", "reduction", "date_add", "date_upd", "name", "customer IDs"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Float.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        scrlGroupList.setViewportView(tblGroupList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrlGroupList, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrlGroupList, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIGroup().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane scrlGroupList;
    private javax.swing.JTable tblGroupList;
    // End of variables declaration//GEN-END:variables
}
